using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wicresoft.SMS.Send
{
    public partial class AddressBook : Form
    {
        public AddressBook()
        {
            InitializeComponent();
        }

        public string TO
        {
            get
            {
                return this.txtTo.Text.Trim();
            }
            set
            {
                this.txtTo.Text = value;
            }
        }

        private bool isOK = false;

        public bool IsOK
        {
            get { return isOK; }
            set { isOK = value; }
        }

        private void btnTo_Click(object sender, EventArgs e)
        {
            SelectNumber();           
        }

        private void lvContacts_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectNumber();
        }

        private void SelectNumber()
        {
            try
            {
                if (this.lvContacts.SelectedItems.Count == 1)
                {
                    string newnumber = this.lvContacts.SelectedItems[0].SubItems[1].Text;
                    this.TO = this.TO.Trim();
                    if (this.TO.Trim().Length != 0)
                        if (this.TO[this.TO.Length - 1] != ';')
                            this.TO += ";";
                    string[] tos = this.TO.Split(';');
                    bool flag = true;
                    foreach (string var in tos)
                    {
                        if (var.Trim() == newnumber)
                        {
                            flag = false;
                            break;
                        }
                    }
                    if (flag)
                        this.TO += this.lvContacts.SelectedItems[0].SubItems[1].Text + ";";
                }
            }
            catch (Exception ex)
            {
                Logger.Exception("AddressBook.SelectNumber ", ex);          
            }
           
        }

        private void AddressBook_Load(object sender, EventArgs e)
        {
            try
            {
                List<ListViewItem> list = ContactsGetter.GetContacts();
                this.lvContacts.Items.AddRange(list.ToArray());
            }
            catch (Exception ex)
            {
                Logger.Exception("AddressBook.AddressBook_Load ", ex);                
            }            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.isOK = true;
            this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.isOK = false;
            this.Close();
        }
    }
}